package com.dev.security.tools.controllers;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.security.tools.beans.Dictionary;
import com.dev.security.tools.beans.DictionaryId;
import com.dev.security.tools.service.IDictionaryService;
import com.dev.security.util.DataGrid;
import com.dev.security.util.Response;

/**
 * @author 飘渺青衣
 * @see
 */
@Controller
@RequestMapping("/dic/")
public class DictionaryController {

	@Autowired
	private IDictionaryService dictionaryService;
	
	/**
	 * 数据字典浏览
	 * @author 飘渺青衣
	 * @version
	 * @param label
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("browse")
	public @ResponseBody DataGrid<Dictionary> browse(String label, int page, int rows) {
		return dictionaryService.findPage(StringUtils.trimToEmpty(label), page, rows);
	}
	
	/**
	 * 字典值浏览
	 * @author 飘渺青衣
	 * @version
	 * @param parent
	 * @return
	 */
	@RequestMapping("children")
	public @ResponseBody DataGrid<Dictionary> children(String parent) {
		return new DataGrid<Dictionary>(
				this.dictionaryService.findDictionaries(StringUtils.trimToEmpty(parent)));
	}
	
	/**
	 * 创建字典
	 * @author 飘渺青衣
	 * @version
	 * @param dic
	 * @return
	 */
	@RequestMapping("create")
	public @ResponseBody Response create(Dictionary dic, DictionaryId id) {
		id.setParent(StringUtils.trimToEmpty(id.getParent()));
		id.setValue(StringUtils.trimToEmpty(id.getValue()));
		dic.setId(id);
		if(this.dictionaryService.exist(
				id.getParent(), id.getValue())) {
			return new Response(false);
		}
		this.dictionaryService.save(dic);
		return new Response(true);
	}
	
	/**
	 * 更新字典
	 * @author 飘渺青衣
	 * @version
	 * @param dic
	 * @return
	 */
	@RequestMapping("update")
	public @ResponseBody Response update(Dictionary dic, DictionaryId id) {
		id.setParent(StringUtils.trimToEmpty(id.getParent()));
		id.setValue(StringUtils.trimToEmpty(id.getValue()));
		dic.setId(id);
		if(this.dictionaryService.exist(
				id.getParent(), id.getValue())) {
			this.dictionaryService.save(dic);
			return new Response(true);
		}
		return new Response(false);
	}
	
	/**
	 * 删除字典
	 * @author 飘渺青衣
	 * @version
	 * @param parent
	 * @param values
	 * @return
	 */
	@RequestMapping("remove")
	public @ResponseBody Response remove(String parent, String[] values) {
		//删除字典值
		this.dictionaryService.remove(values);
		//删除字典本身
		if(this.dictionaryService.remove(StringUtils.trimToEmpty(parent), values) > 0) {
			return new Response(true);
		}
		return new Response(false);
	}
	
	/**
	 * 获取一个字典值
	 * @author 飘渺青衣
	 * @version
	 * @param parent
	 * @param value
	 * @return
	 */
	@RequestMapping("get")
	public @ResponseBody Dictionary get(String parent, String value) {
		return this.dictionaryService.findDictionary(
				StringUtils.trimToEmpty(parent), 
				StringUtils.trimToEmpty(value));
	}
	
	/**
	 * 获取字典值列表
	 * @author 飘渺青衣
	 * @version
	 * @param parent
	 * @return
	 */
	@RequestMapping("list")
	public @ResponseBody List<Dictionary> list(String parent) {
		return this.dictionaryService.findDictionaries(
				StringUtils.trimToEmpty(parent));
	}
}
